﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace Project.module
{
    public partial class frmUpdatemon : Form
    {
        MonAnDTO mon = null;
        public class ComboboxItem
        {
            public string Text { get; set; }
            public int Value { get; set; }

            public override string ToString()
            {
                return Text;
            }
        }
        public frmUpdatemon()
        {
            InitializeComponent();
        }

        private void frmUpdatemon_Load(object sender, EventArgs e)
        {
            mon = (MonAnDTO)this.Tag;
            txtTenmon.Text = mon.Tenmon;
            txtDongia.Text = string.Format("{0:0,0}", float.Parse(mon.Dongia.ToString()));
            LoadLoaiMon();
            LoadDonViTinh();
        }
        private void LoadLoaiMon()
        {
            List<LoaimonDTO> lstloai = LoaiMonBUS.Listloaimon();

            if (lstloai != null)
            {
                foreach (LoaimonDTO loai in lstloai)
                {
                    ComboboxItem itm = new ComboboxItem();
                    itm.Text = loai.Tenloai;
                    itm.Value = loai.Maloai;
                    cbLoaimon.Items.Add(itm);
                }
                cbLoaimon.SelectedText = LoaiMonBUS.TenLoai(mon.Maloai);
            }
        }
        private void LoadDonViTinh()
        {
            List<DonViTinhDTO> lstdv=DonViTinhBUS.Danhsach();
            if (lstdv != null)
            {
                foreach (DonViTinhDTO dv in lstdv)
                {
                    ComboboxItem itm = new ComboboxItem();
                    itm.Text = dv.Tendv;
                    itm.Value = dv.Madv;
                    cbDVT.Items.Add(itm);

                }
                cbDVT.SelectedText = DonViTinhBUS.TenDV(mon.Madv);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MonAnDTO update = new MonAnDTO();
            update.Mamon = mon.Mamon;
            update.Tenmon = txtTenmon.Text;
            update.Dongia = float.Parse(txtDongia.Text);
            if (cbLoaimon.SelectedIndex >= 0)
            {
                update.Maloai = (cbLoaimon.SelectedItem as ComboboxItem).Value;
            }
            else
            {
                update.Maloai = mon.Maloai;
            }
            if (cbDVT.SelectedIndex >= 0)
            {
                update.Madv = (cbDVT.SelectedItem as ComboboxItem).Value;
            }
            else
            {
                update.Madv = mon.Madv;
            }

            try
            {
                MonAnBUS.Capnhat(update);
                this.Close();
            }
            catch (InvalidCastException a)
            {
                throw(a);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
